<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>变量定义</title>
    <script>
        function f1(){
            // 在js中声明变量统一使用var或者let
            var name;
            name = "admin";
            console.log(name);
            name = 1;
            console.log(name);
            name = true;
            console.log(name);
        }
        function f2(){
            var id = 1;
        }
        // 在ES6之前,js没有作用域的概念
        function f3(){
            for(var i = 0; i < 10; i++){
                // console.log(i);
            }
            console.log(i);
        }
        // 相当于下面代码
        function fn(){
            var i;
            for(i = 0; i < 10; i++){

            }
            console.log(i);
        }
        function f4(){
            for(let i = 0; i < 10; i++){
            }
            console.log(i); // 报错
        }
    </script>
</head>
<body>
<button onclick="f1()">先声明后赋值</button><br>
<button onclick="f2()">声明的同时赋值</button><br>
<button onclick="f3()">变量特点</button><br>
<button onclick="f4()">ES6作用域</button><br>
</body>
</html>